package com.github.bobjoy.java.desginpattern;

import java.util.Enumeration;
import java.util.Vector;

/**
 * 观察者模式
 */
public class ObserverDemo {

    public static void main(String[] args) {
        Subject sub = new MySubject();
        sub.add(new Observer1());
        sub.add(new Observer2());

        sub.operation();
    }
}

interface Observer {
    void update();
}

class Observer1 implements Observer {
    @Override
    public void update() {
        System.out.println("observer1 has received!");
    }
}

class Observer2 implements Observer {
    @Override
    public void update() {
        System.out.println("observer2 has received!");
    }
}

interface Subject {
    /** 增加观察者 */
    void add(Observer observer);

    /** 删除观察者 */
    void del(Observer observer);

    /** 通知所有的观察者 */
    void notifyObservers();

    /** 自身的操作 */
    void operation();
}

abstract class AbstractSubject implements Subject {

    private Vector<Observer> vector = new Vector<>();
    @Override
    public void add(Observer observer) {
        vector.add(observer);
    }

    @Override
    public void del(Observer observer) {
        vector.remove(observer);
    }

    @Override
    public void notifyObservers() {
        Enumeration<Observer> enums = vector.elements();
        while(enums.hasMoreElements()){
            enums.nextElement().update();
        }
    }
}

class MySubject extends AbstractSubject {
    @Override
    public void operation() {
        System.out.println("update self!");
        notifyObservers();
    }
}